//
//  MySearchView.m
//  CnInsure
//
//  Created by 王兵 on 16/2/3.
//  Copyright © 2016年 cousy. All rights reserved.
//



#import "MySearchView.h"
#import "DeviceMacro.h"
@interface MySearchView()<UITextFieldDelegate>
@property(nonatomic,weak)UIView *left;
@property(nonatomic,weak)UIImageView *imageView;

@end
@implementation MySearchView
//执行一次block
-(instancetype)init{
    self=[super init];
    if (self) {
        self.font=[UIFont systemFontOfSize:14];
        
        self.delegate=self;
        self.autocorrectionType = UITextAutocorrectionTypeYes;
        self.backgroundColor=[UIColor whiteColor];
        self.textAlignment=NSTextAlignmentCenter;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didchange:) name:UITextFieldTextDidChangeNotification object:self];
        
        
        [self creatLeftView];
        
        
    }
    return self;
}
-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        self.font=[UIFont systemFontOfSize:14];
        self.delegate=self;
        self.autocorrectionType = UITextAutocorrectionTypeYes;
        self.backgroundColor=[UIColor whiteColor];
        self.textAlignment=NSTextAlignmentCenter;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didchange:) name:UITextFieldTextDidChangeNotification object:self];
        
        
        [self creatLeftView];
        
        
    }
    return self;
}
-(void)setIsLowercase:(BOOL)isLowercase{
    _isLowercase=isLowercase;
    if (self.isLowercase) {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
}

-(void)didchange:(NSNotification*)noti{
    UITextField *textField=noti.object;
    NSString *toBeString = textField.text;
    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            textField.text= toBeString.uppercaseString;
        }else{
            // 有高亮选择的字符串
        }
    }
}

-(void)creatLeftView{
    UIView *left=[[UIView alloc]init];
    
    NSString *imageName;
    if ([ICrEAMTool isVW]) {
        imageName=@"search";
    }else{
        imageName=@"skSearch";
    }
    UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
    CGFloat width=25;
    if(IS_IPHONE_5){
        imageView.frame = CGRectMake(70, (self.height-width)/2., width, width);
    }else{
        imageView.frame = CGRectMake(90, (self.height-width)/2., width, width);
    }
    //    imageView.contentMode=UIViewContentModeCenter;
    [left addSubview:imageView];
    self.imageView = imageView;
    self.leftView = left;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    self.leftViewMode = UITextFieldViewModeAlways;
    self.leftView.height = 40;
}

-(void)setTextAlignment:(NSTextAlignment)textAlignment{
    [super setTextAlignment:textAlignment];
    if (textAlignment==NSTextAlignmentLeft) {
        self.imageView.frame=CGRectMake(3, 10, 20, 20);
    }
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    if (!self.isOnceBlock) {
        if (self.goSerach) {
            self.isOnceBlock=YES;
            self.goSerach();
        }
    }
    
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    if (self.isSearch) {
        self.textAlignment=NSTextAlignmentLeft;
        self.leftViewMode=UITextFieldViewModeAlways;
        self.imageView.frame=CGRectMake(3, 10, 20, 20);
    }
    
    
    else [textField resignFirstResponder];
    
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if ([textField.text isEqualToString:@""]) {
        self.textAlignment=NSTextAlignmentCenter;
        if(IS_IPHONE_5){
            self.imageView.frame = CGRectMake(70, 10, 20, 20);
        }else{
            self.imageView.frame = CGRectMake(90, 10, 20, 20);
        }
    }
}
-(CGRect)textRectForBounds:(CGRect)bounds{
    if (self.textAlignment==NSTextAlignmentLeft) {
        bounds.origin.x=22;
    }
    return bounds;
}

-(CGRect)editingRectForBounds:(CGRect)bounds{
    if (self.textAlignment==NSTextAlignmentLeft) {
        bounds.origin.x=22;
    }
    return bounds;
}
@end
